public without sharing class OppDealerController {

	public String backId {get;private set;}
	public String dataJson {get; private set;}
	public String msg {get; private set;}
	public Boolean isErr {get; private set;}
	public Boolean isSuccess {get; private set;}
    private  Map<String,DataUtil.SObjectBean> stageObjectMap;
    public String saveJson {get;set;}

    public OppDealerController() {
        backId = System.currentPageReference().getParameters().get('id');
        stageObjectMap = new Map<String,DataUtil.SObjectBean> ();
    }

    public void init(){

    	try {

    	    if(backId == null )throw new DmlException('页面打开错误!');
    	    if(Id.valueOf(backId).getSobjectType() == Account.SobjectType ){

    	    	Account acc = [select Id,Name,RecordType.DeveloperName from Account where Id =:backId];
    	    	if(!acc.RecordType.DeveloperName.contains('Dealer') )throw new DmlException('只有经销商客户才能新建经销商业务机会!');

    	    	 //字段
	             Map<String,Set<String>> fieldsmap = getFields();
	             System.debug(stageObjectMap);
	             //字段详情
	             Map<String, List<DataUtil.FieldBean>> fbMap = DataUtil.getFieldsBean(fieldsmap,stageObjectMap);

	             for (DataUtil.FieldBean fb : fbMap.get('Opportunity')) {
	             	 
	             	if(fb.name == 'AccountId'){
	             		Map<String,Object> vMap  = new Map<String,Object>();
	             		 vMap.put('Id', acc.Id );
                    	 vMap.put('Name', acc.Name );
	             		 fb.value = vMap;
	             	}
	             	if(fb.name == 'StageName')fb.value = '意向';
	             }
	            Map<String,Object> dataMap = new Map<String,Object>();
	            dataMap.put('base',fbMap);
            	dataMap.put('baseObject',DataUtil.getBaseObjectMap(stageObjectMap)); 
            	dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap), 'UTF-8');
    	    }else{
    	    	throw new DmlException('页面打开错误!');
    	    }

    	}catch (Exception e) {
    		isErr = true;
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()+e.getStackTraceString()));
    	}
    	
    }

     private Map<String,Set<String>>  getFields(){
        Map<String,Set<String>> fieldsMap  = new  Map<String,Set<String>>();
        	
        	String objname = 'Opportunity';
            DataUtil.SObjectBean sb = new DataUtil.SObjectBean();

            sb.title ='业务机会';
            sb.objname = objname;
            sb.stype = DataUtil.getSObjectType(objname);
            sb.type = 'base';
            stageObjectMap.put(objname,sb );
            //安装类型 安装仪器 Supplier__c
            Set<String>  fields = new Set<String>{
            	'AccountId','CloseDate','StageName','WinRatio__c','ProductLineGrade__c','NewName__c',
            	'AnnualContract__c','IntentionalProductRange__c','ProjStartDate__c'
            };

         
            fieldsMap.put(objname, fields);

            

        
        return fieldsMap;
    }

    public void save(){

    	msg = '';
    	try {
    		Map<String,Object> baseMap = (Map<String,Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
    	    Map<String,Set<String>> fieldsMap = getFields();
            Map<String,DataUtil.FieldBean> fbeanMap = DataUtil.getFieldsInfo(Opportunity.SObjecttype,fieldsMap.get('Opportunity'));
            Opportunity opp = new Opportunity();

            for(String field : baseMap.keySet()){

                if(baseMap.containsKey(field))opp.put(field, DataUtil.dataHandler(baseMap.get(field),fbeanMap.get(field).type.toUpperCase() ));
            }
            SavePoint sp = Database.setSavepoint();
            opp.Name = '1';
            opp.RecordTypeId =Utils.getRecordTypeIdByDevNameAndObjName('DealerOpportunity', 'Opportunity');
            try {
                
                insert opp;
                backId = opp.Id;
                msg = '保存成功!';
                isSuccess = true;
            }
            catch (Exception e) {
                Database.rollback(sp);
                msg = '保存失败!'+e.getMessage();
            }
            

    	}
    	catch (Exception e) {
    	    msg = '保存失败:'+e.getMessage();
    	}
    	
    	  
    }




}